﻿using CPPEI.Coolzon.Common;
using CPPEI.Coolzon.Service.Contract;
using Sunboxsoft.ChanceData.ServiceProxy.MainServiceRef;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.UI.Views.EventProxies
{
    public class RespSyncViewStateHandler : BaseResponseHandler<RespSyncViewStateData>
    {
        /// <summary>
        /// 正在执行同步操作
        /// </summary>
        private static bool s_isSyncing = false;
        private static Thread s_syncThread;
        private static Coolzon.ViewState.ViewState s_serViewState;
        private static RemoteEventProxy s_remoteProxy;
        private static object o_lockOpenScene = new object();

        protected override void OnHandle(RemoteEventProxy remoteEventProxy, Response response, RespSyncViewStateData responseData)
        {
            UIContext.Current.IsSynchronizingViewState = false;

            if (remoteEventProxy.RenderView == null)
            {
                return;
            }
            Coolzon.ViewState.ViewState curViewState = remoteEventProxy.RenderView.GetViewState();
            Coolzon.ViewState.ViewState viewState = Coolzon.ViewState.ViewState.FromToken(responseData.ViewStateToken);
            if (remoteEventProxy.RenderView.Solution == null)
            {
                Common.Logger.ErrorToTag("Handler", "接收到同步视图状态的回应，但是RenderView中的方案为空和viewstate里的方案:{0} 不一致，无法执行",
                    viewState.SolutionName);
            }
            else if (remoteEventProxy.RenderView.Solution.Name != viewState.SolutionName)
            {
                Common.Logger.ErrorToTag("Handler", "接收到同步视图状态的回应，但是RenderView中的方案:{0}和viewstate里的方案:{1}不一致，无法执行",
                    remoteEventProxy.RenderView.Solution.Name,
                    viewState.SolutionName);
            }
            else
            {
                if (string.Equals(curViewState.SolutionName, viewState.SolutionName))
                {
                    if (s_syncThread == null)
                    {
                        lock (o_lockOpenScene)
                        {
                            if (s_syncThread == null)
                            {
                                s_remoteProxy = remoteEventProxy;
                                s_serViewState = viewState;
                                s_syncThread = new Thread(Running);
                                s_syncThread.Name = "syncviewstatethread4renderview";
                                s_syncThread.Start();
                            }
                        }
                    }
                }
                else
                {
                    Common.Logger.ErrorToTag("Handler", "接收到同步视图状态的回应，方案:{0}不同于curviewstate里的方案:{1}",
                        viewState.SolutionName,
                        curViewState.SolutionName);
                }
            }
        }

        private static void Running()
        {
            if (s_serViewState.CurrentScene != null)
            {
                Coolzon.ViewState.ScenePath pathNode = s_serViewState.CurrentScene.GetRoot();
                while (pathNode != null)
                {
                    Coolzon.UI.Data.OpenSceneArgs openArgs = Coolzon.UI.Data.OpenSceneArgs.BuildFromString(pathNode.Arguments);
                    // 同步视图状态的时候需要将参数里的清空视图状态参数去除
                    if (openArgs.ParametersString.ContainsKey("sys:isresetviewstate"))
                    {
                        openArgs.ParametersString.Remove("sys:isresetviewstate");
                    }

                    if (pathNode.OpenMode == "open")
                    {
                        s_remoteProxy.RenderView.OpenScene(Data.SwitchMode.Replace,"", "", pathNode.SceneName, openArgs);
                    }
                    else
                    {
                        s_remoteProxy.RenderView.OpenScene(Data.SwitchMode.Pop, "", "", pathNode.SceneName, openArgs);
                    }
                    pathNode = s_serViewState.CurrentScene.GetChild(pathNode);
                    if (pathNode != null)
                    {
                        Thread.Sleep(3000);
                    }
                }
                s_remoteProxy.RenderView.SetViewState(s_serViewState);
            }
            else
            {
                Common.Logger.ErrorToTag("Handler", "接收到同步视图状态的回应，但是viewstate里没有当前打开的场景无法执行");
            }
            s_syncThread = null;
        }
    }
}
